/**
 * 
 */
package poc.android.explistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import poc.android.weather.R;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

/**
 *
 * @author Jason
 * created at: Oct 13, 2011
 */
public class ExpandableListViewDemoWithExpActivity extends ExpandableListActivity {

	static final String TAG="ExpActivity";
	static final Object LOCK=new Object();
	
	Map<Integer, Set<Integer>> mCheckedEntries=new HashMap<Integer, Set<Integer>>();	
	Button mBtnExpTest;
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
		int groupPosition, int childPosition, long id) {
		//return super.onChildClick(parent, v, groupPosition, childPosition, id);
		
		/*CheckBox aCbo=(CheckBox) v.findViewById(R.id.check1);
		boolean aCboState=false;
		if (aCbo!=null) {
			aCboState=!aCbo.isChecked();
			aCbo.setChecked(aCboState);
			// update the status... to be used on the onExpand... method
			this.updateCheckedEntries(groupPosition, childPosition, aCboState);
		}
		Log.w(TAG, "grpPosition -> 0 probably: "+groupPosition+"   childIndex -> "+childPosition);*/
		
		TextView aStatus=(TextView) v.findViewById(R.id.status);
		boolean aStatusState=false;
		if (aStatus!=null) {
			String aText=aStatus.getText().toString();
			if (aText==null || aText.trim().length()==0) {
				aStatus.setText("Checked");
				aStatusState=true;
				
			} else if (aText.equals("Checked")) {
				aStatus.setText("");
				aStatusState=false;
			}
			this.updateCheckedEntries(groupPosition, childPosition, aStatusState);
		}
		return true;
	}

	/**
	 * method to return the requested Set
	 * @param pGroupPosition
	 * @return
	 */
	private Set<Integer> getCheckedIndexSet(int pGroupPosition) {
		Integer aGrpKey=Integer.valueOf(pGroupPosition);
		
		return this.mCheckedEntries.get(aGrpKey);
	}
	
	/**
	 * method to note down ... which index should be enabled (checkbox)
	 * @param pGroupPosition
	 * @param pChildPosition
	 * @param pEnabled
	 */
	private void updateCheckedEntries(int pGroupPosition, int pChildPosition, boolean pEnabled) {
		Set<Integer> aCheckedIndex=null;
		Integer aGrpKey=Integer.valueOf(pGroupPosition);
		Integer aChildIdx=Integer.valueOf(pChildPosition);
		
		aCheckedIndex=this.getCheckedIndexSet(pGroupPosition);
		if (aCheckedIndex==null) {
			aCheckedIndex=new HashSet<Integer>();
			synchronized(LOCK) {
				this.mCheckedEntries.put(aGrpKey, aCheckedIndex);
			}
		}
		
		if (pEnabled) {
			if (!aCheckedIndex.contains(aChildIdx)) {
				aCheckedIndex.add(aChildIdx);
			}
		} else {
			if (aCheckedIndex.contains(aChildIdx)) {
				aCheckedIndex.remove(aChildIdx);
			}
		}
		/*
		 * debug usage
		 */
		Log.w(TAG, "set is now... "+pGroupPosition+" :: "+aCheckedIndex);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onGroupExpand(int groupPosition) {
		//super.onGroupExpand(groupPosition);
		this.updateSelectedChild(groupPosition, true);
	}

	private void updateSelectedChild(int pGroupPosition, boolean pExpandList) {
		Set<Integer> aSet=this.getCheckedIndexSet(pGroupPosition);		
		if (aSet!=null) {
			// reset all check(s) in case.. invalid...
			for (Integer aChildPos:aSet) {				
				this.getExpandableListView().setSelectedChild(pGroupPosition, aChildPos.intValue(), pExpandList);
			}
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.exp_xx_activity);

		this.setupWidgets(savedInstanceState);
	}
	
	/**
	 * 
	 * @param pBundle
	 */
	private void setupWidgets(Bundle pBundle) {
		SimpleExpandableListAdapter aExpListAdapter =
			new SimpleExpandableListAdapter(
				this,
				createGroupList(),	// groupData describes the first-level entries
				R.layout.exp_xx_group,	// Layout for the first-level entries
				new String[] { "group" },	// Key in the groupData maps to display
				new int[] { R.id.childname },		// Data under "colorName" key goes into this TextView
				createChildList(),	// childData describes second-level entries
				R.layout.exp_xx_child,	// Layout for second-level entries
				new String[] { "brandId" },	// Keys in childData maps to display
				new int[] { R.id.childname }	// Data under the keys above go into these TextViews
			);
		//R.id.childname
		this.setListAdapter(aExpListAdapter);
		this.getExpandableListView().setFastScrollEnabled(true);
		
		this.mBtnExpTest=(Button) this.findViewById(R.id.btnExpTest);
		this.mBtnExpTest.setOnClickListener(new Button.OnClickListener() {
			
			public void onClick(View v) {
				onTestClick(v);
			}}
		);
	}
	
	/**
	 * click event on the "Test+Get" button
	 * @param pView
	 */
	private void onTestClick(final View pView) {
		Toast.makeText(this, this.getCheckedIndexSet(0).toString(), Toast.LENGTH_LONG);
	}
	
	/**
	 * method to create the Group... for the ExpandableListView
	 * @return
	 */
	private List<Map<String, String>> createGroupList() {
		List<Map<String, String>> aGrpList=new ArrayList<Map<String, String>>();
		Map<String, String> aItemMap=new HashMap<String, String>();
		
		aItemMap.put("group", " Brand name:");
		aGrpList.add(aItemMap);
		
		return aGrpList;
	}
	
	/**
	 * method to create the Children... for the ExpandableListView
	 * @return
	 */
	private List<List<Map<String, Object>>> createChildList() {
		List<List<Map<String, Object>>> aChildList=new ArrayList<List<Map<String, Object>>>();
		List<Map<String, Object>> aItemsList=new ArrayList<Map<String, Object>>();
		Map<String, Object> aItemMap=null;
		
		for (int i=0; i<5; i++) {
			aItemMap=new HashMap<String, Object>();
			aItemMap.put("brandId", "BRA-"+(i+1));
			aItemMap.put("brandDesc", "Description for BRA-"+(i+1));
			aItemsList.add(aItemMap);
		}
		aChildList.add(aItemsList);
		
		return aChildList;
	}
	
	// onPause() onResume...
}
